// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "TUCoreTypes.h"
#include "TUWeaponComponent.generated.h"

class ATUBaseWeapon;

UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class THEMUP_API UTUWeaponComponent : public UActorComponent
{
	GENERATED_BODY()

public:	
	UTUWeaponComponent();
	virtual void StartFire();
	virtual void NextWeapon();
	virtual void StopFire();
	bool IsFiring() const;
	void Reload();
	bool GetCurrentWeaponUIData(FWeaponUIData& UIData) const;
	bool GetCurrentWeaponAmmoData(FAmmoData& AmmoData) const;
	bool TryToAddAmmo(TSubclassOf<ATUBaseWeapon> WeaponType, int32 ClipsAmount);
protected:
	UPROPERTY()
	ATUBaseWeapon* CurrentWeapon = nullptr;
	UPROPERTY()
	TArray<ATUBaseWeapon*> Weapons;

	UPROPERTY()
	UAnimMontage* CurrentReloadAnimMontage = nullptr;
	UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Weapon")
	TArray<FWeaponData> WeaponData;
	UPROPERTY(EditDefaultsOnly, Category = "Weapon")
	FName WeaponArmorySocketName = "ArmorySocket";
	UPROPERTY(EditDefaultsOnly, Category = "Weapon")
	FName WeaponEquipSocketName = "WeaponSocket";
	UPROPERTY(EditDefaultsOnly, Category = "Animation")
	UAnimMontage* EquipAnimMontage;

	virtual void BeginPlay() override;
	virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
	bool CanFire() const;
	bool CanEquip() const;
	void EquipWeapon(int32 WeaponIndex);
	int32 CurrentWeaponIndex = 0;

private:
	void SpawnWeapons();
	void AttachWeaponToSocket(ATUBaseWeapon* Weapon, USceneComponent* SceneComponent, const FName& SocketName);
	void PlayAnimMontage(UAnimMontage* Animation);
	void InitAnimations();
	void OnEquipFinished(USkeletalMeshComponent* MeshComp);
	void OnReloadFinished(USkeletalMeshComponent* MeshComp);
	bool CanReload() const;
	void ChangeClip();
	void OnClipEmpty(ATUBaseWeapon* ClipEmptyWeapon);

	bool EquipAnimInProgress = false;
	bool ReloadAnimInProgress = false;
		
};
